package TestExercises07;

//多线程练习2(送礼品)

/*
有100份礼品,两人同时发送，当剩下的礼品小于10份的时候则不再送出。
利用多线程模拟该过程并将线程的名字和礼物的剩余数量打印出来
 */

public class Test02 {
    public static void main(String[] args) {

        MyThread02 t1 = new MyThread02();
        MyThread02 t2 = new MyThread02();

        t1.setName("第一人");
        t2.setName("第二人");

        t1.start();
        t2.start();
    }
}

class MyThread02 extends Thread {

    static int gift = 100;

    static Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (obj) {
                if (gift >= 10) {
                    System.out.println(getName() + "送的是第" + (101 - gift) + "份礼品");
                    gift--;
                    System.out.println("剩余" + gift + "份礼品");
                } else {
                    break;
                }
            }
        }
    }
}
